package com.xteam.xnetworkdisk.sys.service.impl;

import com.xteam.xnetworkdisk.common.CurrentUser;
import com.xteam.xnetworkdisk.utils.PageUtils;
import com.xteam.xnetworkdisk.utils.Query;
import com.xteam.xnetworkdisk.utils.R;
import com.xteam.xnetworkdisk.vo.SettingsInfoVO;
import com.xteam.xnetworkdisk.vo.SettingsModifyVO;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xteam.xnetworkdisk.sys.dao.SettingsDao;
import com.xteam.xnetworkdisk.sys.entity.SettingsEntity;
import com.xteam.xnetworkdisk.sys.service.SettingsService;


@Service("settingsService")
public class SettingsServiceImpl extends ServiceImpl<SettingsDao, SettingsEntity> implements SettingsService {

    private final SettingsDao settingsDao;

    public SettingsServiceImpl(SettingsDao settingsDao) {
        this.settingsDao = settingsDao;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SettingsEntity> page = this.page(
                new Query<SettingsEntity>().getPage(params),
                new QueryWrapper<SettingsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R getSettingsInfo() {
        Integer currentUserId = CurrentUser.getCurrentUserId();
        QueryWrapper<SettingsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", currentUserId);
        SettingsEntity settingsEntity = this.baseMapper.selectOne(wrapper);
        if (settingsEntity == null) {
            return R.error("未生成settings");
        }

        SettingsInfoVO settingsInfoVO = new SettingsInfoVO();
        settingsInfoVO.setEmail(settingsEntity.getEmail());
        settingsInfoVO.setLoginIp(settingsEntity.getLoginIp());
        settingsInfoVO.setLoginAdress(settingsEntity.getLoginAdress());
        settingsInfoVO.setSyncMode(settingsEntity.getSyncMode());
        settingsInfoVO.setAbnormalEmailAlert(settingsEntity.getAbnormalEmailAlert());
        settingsInfoVO.setDarkMode(settingsEntity.getDarkMode());

        return R.ok().put("settingsInfo", settingsInfoVO);
    }

    @Override
    public R modSettingsInfo(SettingsModifyVO settingsModityVo) {
        Integer darkMode = settingsModityVo.getDarkMode();
        Integer syncMode = settingsModityVo.getSyncMode();
        Boolean abnormalEmailAlert = settingsModityVo.getAbnormalEmailAlert();

        Integer currentUserId = CurrentUser.getCurrentUserId();
        QueryWrapper<SettingsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", currentUserId);
        SettingsEntity settingsEntity = this.baseMapper.selectOne(wrapper);
        if (settingsEntity == null) {
            return R.error("未生成settings");
        }
        if(darkMode != null){
            settingsEntity.setDarkMode(darkMode);
        }
        if(syncMode != null){
            settingsEntity.setSyncMode(syncMode);
        }
        if(abnormalEmailAlert != null){
            settingsEntity.setAbnormalEmailAlert(abnormalEmailAlert);
        }
        int i = settingsDao.updateById(settingsEntity);
        if (i == 0) {
            return R.error("未修改任何属性");
        }

        return R.ok();
    }

}